//
//  CusTableViewCell.m
//  TestDemo
//
//  Created by apple on 2019/3/18.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "CusTableViewCell.h"
#import "TextCollectionViewCell.h"
#import <Masonry.h>
@interface CusTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end
@implementation CusTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self.contentView addSubview:self.textV];
        [self.textV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView);
            make.top.mas_equalTo(self.contentView);
            make.right.mas_equalTo(self.contentView);
            make.height.mas_equalTo(100);
        }];
        [self.contentView addSubview:self.collectV];
        [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.textV.mas_bottom);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(50);//先随定一个
        }];
    }
    return self ;
}
-(UICollectionView *)collectV{
    if (!_collectV) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection =     UICollectionViewScrollDirectionVertical;
        _collectV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectV.delegate = self;
        _collectV.dataSource = self;
        
        [_collectV registerClass:[TextCollectionViewCell class] forCellWithReuseIdentifier:@"TextCollectionViewCell"];
        _collectV.backgroundColor = [UIColor yellowColor];
        
    }
    return _collectV;
}
-(UITextView *)textV
{
    if(_textV == nil){
        _textV = [[UITextView alloc] initWithFrame:CGRectZero];
        _textV.backgroundColor = [UIColor redColor];
    }
    return _textV;
}
-(void)setDataArr:(NSArray *)dataArr{
    [self.collectV reloadData]; //重新换数据源的时候 记得重回用的cell上的colletionView重新加载数据
    self.hightED = 0; //当重新换数据源的时候 初始化自己的高度. (如果不写 就有一种意外比如 比如一个cell被重用,开始这个cell的collectionView的cell 和重用之后是一样的  self.hightED != hight  重用之前 和重用之后的内容高度 很定是一样的啊 那么他的高度是不用跟新 但是更新tableViewCell的高度的 代理方法还是 要走吧)
    _dataArr = dataArr;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
     return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TextCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TextCollectionViewCell" forIndexPath: indexPath];
    cell.backgroundColor = [UIColor blackColor];
    
    [self updateCollectionViewHight:self.collectV.collectionViewLayout.collectionViewContentSize.height];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(80, 80);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}


-(void)updateCollectionViewHight:(CGFloat)hight{
    
    NSLog(@"+%@",self);
    NSLog(@"++ %f",self.hightED);
    NSLog(@"+++ %f",hight);
    
    if (self.hightED != hight) { //这个判断起到两个作用 第一 以为这个方法被调用多次这样写 保证 每个cell里面调用一次,切只调用一次  第二是当cell被重用从用的cell上的collectionView内容高度不一样的时候重新 更新跟新高度
        self.hightED = hight;
        
        NSLog(@"+++++%ld",self.indexPath.row);
        [self.collectV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(hight);
        }];
        
        if (_deleget && [_deleget respondsToSelector:@selector(uodataTableViewCellHight:andHight:andIndexPath:)]) {
            [self.deleget uodataTableViewCellHight:self andHight:hight andIndexPath:self.indexPath];
        }
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
